<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Redenvelopes extends Common
{
	
	function index()
	{
    	$where = array();
    	$order='id desc';
		$State = input('get.State');
        $StarAddTime= input('get.StarAddTime');
        $EndAddTime = input('get.EndAddTime');
    	$Uname = input('get.Uname');
    	if($Uname){
			$ids = model('User')->queryUserIds($Uname);
    		$where['User_id'] = ['in',$ids];
    	}
		if($StarAddTime || $EndAddTime){
            if(strtotime($EndAddTime)<strtotime($StarAddTime)){
                return '<script>alert("请选择正确的时间"),history.go(-1)</script>';
            }       
			$where['Addtime'] =array(['>=',strtotime($StarAddTime)],['<=',strtotime($EndAddTime)]);
        }
        if($State){
        	$where['State']=['=',$State];
        }
		$arr = [
			'Uname'=>$Uname,
			'State'=>$State,
			'StarAddTime'=>$StarAddTime,
			'EndAddTime'=>$EndAddTime,
		];
		$pageListr = model('Redenvelopes')->getRedenvelopesPage($where,$order,$arr,12);
        $redenvelopes_list = $pageListr->all(); 
		$arr['pageListr'] = $pageListr;
		$arr['redenvelopes_list'] = $redenvelopes_list;
		return view('index',$arr);
	}
	function card($id){
		$vm = model('Redenvelopes')->gerRedenvelopesInfo($id);
		return view('card',['redenvelopes_info'=>$vm]);
	}
	public function deleteRedenvelopes($id)
	{
		return model('Redenvelopes')->deleteRedenvelopes($id);
	}
	public function toexamineRedenvelopes($id)
	{
		if (!$this->REQUEST->isAjax()) return "非法操作";
		$sta = 1;
		return model('Redenvelopes')->toexamineRedenvelopes($id,$sta);
	}

	//红包领取记录
	public function code($pagesize = 12)
	{
		$where = array();
    	$order='id desc';
        $StarAddTime= input('get.StarAddTime');
        $EndAddTime = input('get.EndAddTime');
    	$Uname = input('get.Uname');
		$Pname = input('get.Pname');
		$Redid = input('get.Redid');
    	if($Uname){
			$ids = model('User')->queryUserIds($Uname);
    		$where['User_id'] = ['in',$ids];
    	}
		if($Pname){
			$ids = model('Redenvelopes')->queryRedenvelopesIds($Pname);
    		$where['Redenvelopes_id'] = ['in',$ids];
    	}
		if($Redid){
    		$where['Redenvelopes_id'] = ['like','%'.$Redid.'%'];
    	}
		if($StarAddTime || $EndAddTime){
            if(strtotime($EndAddTime)<strtotime($StarAddTime)){
                return '<script>alert("请选择正确的时间"),history.go(-1)</script>';
            }       
			$where['Addtime'] =array(['>=',strtotime($StarAddTime)],['<=',strtotime($EndAddTime)]);
        }
		$arr = [
			'Uname'=>$Uname,
			'Pname'=>$Pname,
			'Redid'=>$Redid,
			'StarAddTime'=>$StarAddTime,
			'EndAddTime'=>$EndAddTime,
		];
		$pageListr = Db::name('redenvelopes_code')->where($where)->order($order)->paginate($pagesize,false,['query'=>$arr]);
        $redenvelopes_list = $pageListr->all(); 
		foreach($redenvelopes_list as $v=>$k){
			$redenvelopes_list[$v]['Recipients'] = getUserInfo($k['User_id']);
			$redenvelopes_list[$v]['Publisher'] = getUserInfo(getRedenvelopesInfo($k['Redenvelopes_id'],'User_id'));
		}
		$arr['pageListr'] = $pageListr;
		$arr['redenvelopes_code_list'] = $redenvelopes_list;
		return view('code',$arr);
	}
	//删除红包领取记录
	public function deleteRedenvelopesCode($id)
	{
		if (!$this->REQUEST->isAjax()) return "非法操作";
		$where['id'] = ['in',$id];
		if(Db::name('redenvelopes_code')->where($where)->delete())
		return json(['code'=>1,'msg'=>'删除成功']);
		return json(['code'=>0,'msg'=>'删除失败']);
	}
}
